﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LotteryText {
    public partial class MainForm : Form {
        private string fileNameT1 = string.Empty;
        private string fileNameT2 = string.Empty;
        private List<List<string>> m_dataT1;
        private List<List<string>> m_dataT2;

        public MainForm() {
            InitializeComponent();

            labelInfoT1.Text = labelInfoT2.Text = string.Empty;
            oldColor = textBoxQiShu.BackColor;
        }

        private void ShowInfo(string info) {
            MessageBox.Show(info, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void buttonLoadT1_Click(object sender, EventArgs e) {
            if (openFileDialog.ShowDialog() != DialogResult.OK) {
                return;
            }
            fileNameT1 = openFileDialog.FileName;
            labelInfoT1.Text = "表1：" + openFileDialog.FileName;

            var form = new WaitForm();
            form.SetInfo("正在导入表1，请稍等...");
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += (a, b) => {
                m_dataT1 = ExcelHelper.LoadT1(fileNameT1);
            };

            bw.RunWorkerCompleted += (a, b) => {
                form.Hide();
                form.Close();
                if (b.Error == null) {
                    showT1Data();
                } else {
                    this.ShowInfo(b.Error.Message);
                }
            };
            bw.RunWorkerAsync();
            form.ShowDialog();
        }

        private void buttonLoadT2_Click(object sender, EventArgs e) {
            if (openFileDialog.ShowDialog() != DialogResult.OK) {
                return;
            }

            fileNameT2 = openFileDialog.FileName;
            labelInfoT2.Text = "表2：" + openFileDialog.FileName;

            var form = new WaitForm();
            form.SetInfo("正在导入表2，请稍等...");
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += (a, b) => {
                m_dataT2 = ExcelHelper.LoadT2(fileNameT2);
            };

            bw.RunWorkerCompleted += (a, b) => {
                form.Hide();
                form.Close();
                if (b.Error == null) {
                    this.ShowInfo("成功导入表2的数据");
                } else {
                    this.ShowInfo(b.Error.Message);
                }
            };
            bw.RunWorkerAsync();
            form.ShowDialog();
        }

        private void buttonSave_Click(object sender, EventArgs e) {
            var qiShu = textBoxQiShu.Text;
            if (string.IsNullOrEmpty(qiShu)) {
                MessageBox.Show("没有输入期数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBoxQiShu.Focus();
                return;
            }

            if (string.IsNullOrEmpty(fileNameT1)) {
                MessageBox.Show("没有选择表1", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (string.IsNullOrEmpty(fileNameT2)) {
                MessageBox.Show("没有选择表2", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show("请确认是否生成上次预测文字分布表", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) != DialogResult.Yes) {
                return;
            }

            var form = new WaitForm();
            form.SetInfo("正在生成上次预测文字分布，请稍等...");
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += (a, b) => {
                ExcelHelper.Gen(qiShu, m_dataT1, m_dataT2);
            };

            bw.RunWorkerCompleted += (a, b) => {
                form.Hide();
                form.Close();
                if (b.Error == null) {
                    //this.ShowInfo("成功生成上次预测文字分布");
                } else {
                    this.ShowInfo(b.Error.Message);
                }
            };
            bw.RunWorkerAsync();
            form.ShowDialog();

        }

        private void showT1Data() {
            if (m_dataT1 == null) return;

            grid.ColumnCount = 14;
            grid.RowCount = 2;
            for (int row = 0; row < m_dataT1.Count; row++) {
                for (int col = 0; col < m_dataT1[row].Count; col++) {
                    grid[col, row].Value = m_dataT1[row][col];
                }
            }

            grid.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;

            grid.Rows[0].HeaderCell.Value = "本次场次号排序";
            grid.Rows[1].HeaderCell.Value = "上次场次号排序";

            for (var i = 0; i < 14; i++)
                grid.Columns[i].HeaderText = (i + 1).ToString();

            grid.AutoResizeColumns();
        }

        private Color oldColor;

        private void textBoxQiShu_Leave(object sender, EventArgs e) {
            textBoxQiShu.BackColor = oldColor;
        }

        private void textBoxQiShu_Enter(object sender, EventArgs e) {
            textBoxQiShu.BackColor = Color.Aqua;
        }
    }
}
